package com.zzb.projectfactory.framework.param.controller;

import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.param.entity.SysParam;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.param.form.SysParamSearchForm;
import com.zzb.projectfactory.framework.param.service.SysParamService;
import com.zzb.projectfactory.framework.common.BasePage;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 系统配置表controller
*/
@Controller
@RequestMapping("/system/sysParam")
public class SysParamController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysParamController.class);
    @Autowired
    private SysParamService sysParamService;

    /**系统配置表管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "system/sysParam/index";
    }
    /**系统配置表列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysParam> list(SysParamSearchForm sysParamSearchForm){
        return sysParamService.queryPage(sysParamSearchForm, SysParam.class);
    }

    /**系统配置表表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysParam sysParam;
        if(StringUtils.isEmpty(id)){
            sysParam = new SysParam();
    } else{
        sysParam = sysParamService.get(id);
        if(sysParam == null) sysParam = new SysParam();
    }
        model.addAttribute("sysParam",sysParam);
        return "system/sysParam/form";
    }

    /**系统配置表保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysParam> save(SysParam sysParam , HttpServletRequest request){
        try {
            User user = getCurrentUser(request);
            return sysParamService.saveSysParam(sysParam, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**系统配置表删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return sysParamService.deleteSysParam(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**系统配置表开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return sysParamService.recovery(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**系统配置表屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return sysParamService.trash(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
